#include <iostream>
#include <string.h>
#include "link.h"
using namespace std;
void help(void)
{
    cout << "帮助文档" << endl;
    cout << "head_insert:头插法将节点插入链表" << endl;
    cout << "tail_insert:尾插法将节点插入链表" << endl;
    cout << "insert:自定义位置将节点插入链表" << endl;
    cout << "deleteNode:删除节点" << endl;
    cout << "help:显示帮助文档" << endl;
    cout << "cls:清空屏幕" << endl;
    cout << "exit:退出程序" << endl;
    cout << "print:遍历输出链表" << endl;
    cout << "search:查询节点" << endl;
}
Node *head_insert(Node *node, Node *head) // 这里的node要使用引用，不然每次都是函数自己开辟的node空间地址
{
    if (head != NULL)
    {
        node->next = head;
        head = node;
        return head;
    }
    else
    {
        head = node;
        return head;
    }
}
Node *tail_insert(Node *node, Node *head)
{
    if (head == NULL)
    {
        head = node;
        return head;
    }
    Node *cur_node = head;
    while (cur_node->next != NULL)
    {
        cur_node = cur_node->next;
    }
    cur_node->next = node;
    return head;
}
Node *insert(Node *node, Node *head, int loc) // 这里默认位置0为第一个位置
{
    if (head == NULL)
    {
        head = node;
        return head;
    }
    int id = 0;
    Node *cur_node = head;
    Node *pre_node = head;
    while (id < loc)
    {
        pre_node = cur_node; // 双指针，一个指向当前节点，一个指向上一节点
        cur_node = cur_node->next;
        id += 1;
    }
    if (pre_node == cur_node) // 头插法
    {
        node->next = cur_node;
        head = node;
        return head;
    }
    pre_node->next = node;
    node->next = cur_node; // 尾插法，中间插入
    return head;
}
Node *deleteNode(Node *head, int loc) // 也是默认0为第一个位置
{
    int id = 0;
    Node *cur_node = head;
    Node *pre_node = head;
    while (id < loc)
    {
        pre_node = cur_node; // 双指针，一个指向当前节点，一个指向上一节点
        cur_node = cur_node->next;
        id += 1;
    }
    if (pre_node == cur_node) // 删除头节点
    {
        head = cur_node->next;
        delete cur_node;
        return head;
    }
    pre_node->next = cur_node->next;
    delete cur_node; // 删除尾节点或指定节点
    return head;
}
void cls()
{
    system("cls");
}
int exit()
{
    exit(0);
}
void print(Node *head)
{
    Node *cur_node = head;
    if (cur_node == NULL)
    {
        cout << "链表为空" << endl;
    }
    else
    {
        while (cur_node != NULL)
        {
            cout << cur_node->data << "   " << cur_node->name << endl;
            cur_node = cur_node->next;
        }
    }
}
void serach(Node *head, int loc) // 寻找第loc个位置的节点，并输出信息
{
    if (head == NULL)
    {
        cout << "链表为空，无法搜索" << endl;
    }
    int id = 0;
    Node *cur_node = head;
    while (id < loc) // 这里没有写异常处理报错，所以loc不能超过链表长度
    {
        id += 1;
        cur_node = cur_node->next;
    }
    cout << cur_node->data << " " << cur_node->name << endl;
}
Node *head = NULL;
void run()
{

    help();
    while (1)
    {
        cout << "请输入操作指令:";
        char cmd[32];
        cin >> cmd;
        if (strcmp(cmd, "head_insert") == 0)
        {
            Node *node = new Node; // 新开辟空间，不然每次的空间都是一样
            node->next = NULL;     // 记得对指针的初始化，一定要指向NULL，不然就会有野指针存在
            cout << "请输入插入节点数据和名字:" << endl;
            cin >> node->data >> node->name;
            head = head_insert(node, head);
        }
        else if (strcmp(cmd, "tail_insert") == 0)
        {
            Node *node = new Node;
            node->next = NULL;
            cout << "请输入插入节点数据和名字:" << endl;
            cin >> node->data >> node->name;
            head = tail_insert(node, head);
        }
        else if (strcmp(cmd, "insert") == 0)
        {
            Node *node = new Node;
            node->next = NULL;
            int loc;
            cout << "请输入插入节点数据,名字和插入位置:" << endl;
            cin >> node->data >> node->name >> loc;
            head = insert(node, head, loc);
        }
        else if (strcmp(cmd, "deleteNode") == 0)
        {
            int loc;
            cout << "请输入要删除的节点位置:";
            cin >> loc;
            head = deleteNode(head, loc);
        }
        else if (strcmp(cmd, "help") == 0)
        {
            help();
        }
        else if (strcmp(cmd, "cls") == 0)
        {
            cls();
        }
        else if (strcmp(cmd, "exit") == 0)
        {
            exit();
        }
        else if (strcmp(cmd, "print") == 0)
        {
            print(head);
        }
        else if (strcmp(cmd, "search") == 0)
        {
            int loc;
            cout << "请输入要查询的节点位置:";
            cin >> loc;
            serach(head, loc);
        }
    }
}

#if 0
int main()
{
    run();
}
#endif